package com.example.end.mydairy;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.end.DaoMaster;
import com.end.DaoSession;
import com.end.Note;
import com.end.NoteDao;

import java.text.DateFormat;
import java.util.Date;

public class Editor extends AppCompatActivity{
    private static final String TAG = "Diary";
    public static final String EDIT_DIARY_ACTION = "com.end.EDIT_DIARY";
    public static final String INSERT_DIARY_ACTION = "con.end.INSERT_DIARY";

    private EditText mtitle;
    private EditText mbody;
    private Button confirm;
    private Toolbar toolbar;

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private NoteDao noteDao;
    private Note note;
    private Cursor cursor;

    private long mid;
    private int mState;
    private static final int STATE_EDIT = 0;
    private static final int STATE_INSERT = 1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editor);
        final Intent intent=getIntent();
        final String action=intent.getAction();
        mtitle=(EditText)findViewById(R.id.title);
        mbody=(EditText)findViewById(R.id.body);
        confirm=(Button)findViewById(R.id.confirm);
        initdao();
        if(EDIT_DIARY_ACTION.equals(action))
        {
            Bundle bundle=new Bundle();
            bundle=this.getIntent().getExtras();
            mid=bundle.getLong("id");
            note=noteDao.loadByRowId(mid);
            mtitle.setTextKeepState(note.getTitle());
            mbody.setTextKeepState(note.getContent());
            mState=STATE_EDIT;
            setTitle("编辑日记：");
        }
        else if(INSERT_DIARY_ACTION.equals(action))
        {
            mState=STATE_INSERT;
            setTitle("新建日记：");
        }
        else
        {
            Log.e(TAG,"没有这样的行为！");
            finish();
            return;
        }
        confirm.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view)
            {
                if(mState==STATE_INSERT)
                {
                    insertDiary();
                }
                else if(mState==STATE_EDIT)
                {
                    updateDiary();
                }
                Intent intent=new Intent(Editor.this,MainActivity.class);
                startActivity(intent);
                Editor.this.finish();
            }
        });
    }

    private void initdao() {
        DaoMaster.DevOpenHelper helper=new DaoMaster.DevOpenHelper(this,"notes-db",null);
        db=helper.getWritableDatabase();
        daoMaster=new DaoMaster(db);
        daoSession=daoMaster.newSession();
        noteDao=daoSession.getNoteDao();
    }
    private void insertDiary() {
        String title=mtitle.getText().toString();
        String content=mbody.getText().toString();
        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        String date = df.format(new Date());
        Note note=new Note(null,title,content,date);
        noteDao.insert(note);
    }
    private void updateDiary() {
        String title=mtitle.getText().toString();
        String content=mbody.getText().toString();
        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        String date = df.format(new Date());
        note.setTitle(title);
        note.setContent(content);
        note.setDate(date);
        noteDao.update(note);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.menu_edit,menu);
        super.onCreateOptionsMenu(menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
//            case R.id.share:
//                share();
//                break;
            case R.id.cancel:
                Intent intent=new Intent(Editor.this,MainActivity.class);
                startActivity(intent);
                Editor.this.finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    public void onBackPressed(){
        if(mState==STATE_INSERT)
        {
            insertDiary();
        }
        else if(mState==STATE_EDIT)
        {
            updateDiary();
        }
        Intent intent=new Intent(Editor.this,MainActivity.class);
        startActivity(intent);
        Editor.this.finish();
    }
//    public void share()
//    {
//        Intent textIntent = new Intent(Intent.ACTION_SEND);
//        textIntent.setType("text/plain");
//        textIntent.putExtra(Intent.EXTRA_TEXT,"标题："+mtitle.getText().toString()+"\n"
//        +"正文："+mbody.getText().toString());
//        startActivity(Intent.createChooser(textIntent, "分享"));
//    }
}
